package com.comment.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comment.entity.ClientComments;
import com.comment.mapper.ClientCommentsMapper;
import com.comment.service.IClientCommentsService;
import com.common.untils.CommonResult;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pgx
 * @since 2022-09-09
 */
@Service
public class ClientCommentsServiceImpl extends ServiceImpl<ClientCommentsMapper, ClientComments> implements IClientCommentsService {
    @Autowired
    private ClientCommentsMapper clientCommentsMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public CommonResult selectAll(String goodsId, String shopId) {
        QueryWrapper<ClientComments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodsId);
        queryWrapper.eq("shop_id",shopId);
        List<ClientComments> clientComments = clientCommentsMapper.selectList(queryWrapper);
        return new CommonResult(2000,"查询成功",clientComments);
    }

    @Override
    public CommonResult insertComment(ClientComments clientComments,String hometoken) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String s = opsForValue.get(hometoken);
        Map map = JSONObject.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        String username = client.getString("username");
        clientComments.setUid(username);
        int insert = clientCommentsMapper.insert(clientComments);
        if (insert==0){
            return new CommonResult(5000,"评论失败",null);
        }
        return new CommonResult(2000,"评论成功",null);
    }
}
